from django.db import models


# Create your models here.
# ''' 用户信息表 '''
class UserInfo(models.Model):
    user_name = models.CharField(verbose_name="用户名", max_length=32)
    email = models.CharField(verbose_name="邮箱", max_length=32)
    mobile_phone = models.CharField(verbose_name="手机号", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=32)

    def __str__(self):
        return self.user_name


# """价格策略表"""
class Price(models.Model):
    category_choices = (
        (1, '免费版'),
        (2, '收费版'),
        (3, '其他版'),
    )
    category = models.SmallIntegerField(verbose_name="收费类型", default=1, choices=category_choices)
    title = models.CharField(verbose_name="标题", max_length=32)
    unit_price = models.DecimalField(verbose_name="价格/年", max_digits=6, decimal_places=2)  # 要存储的数字为：最大长度为6位数，小数点后2位小数
    # 类似 IntegerField 类型, 但必须是整数或者零 (0)。 取值范围是 0 到 2147483647 。 允许 0 是为了向后兼容。
    project_total = models.PositiveIntegerField(verbose_name="创建项目总个数")
    project_members = models.PositiveIntegerField(verbose_name="每个项目成员数")
    space_size = models.PositiveIntegerField(verbose_name="每个项目大小")
    file_size = models.PositiveIntegerField(verbose_name="单文件大小")
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)


# """交易订单表"""
class OrderInfo(models.Model):
    status_choices = (
        (1, '未支付'),
        (2, '已支付'),
    )
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choices)
    order_num = models.CharField(verbose_name="订单号", max_length=64, unique=True)  # 唯一索引
    create_user = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="用户")
    price = models.ForeignKey(Price, on_delete=models.CASCADE, verbose_name="价格")
    amount = models.IntegerField(verbose_name="数量", help_text='0表示无限期')
    payment = models.DecimalField(verbose_name="实际支付金额", max_digits=10, decimal_places=2)  # 要存储的数字为：最大长度为10位数，小数点后2位小数
    start_time = models.DateTimeField(verbose_name="生效时间", null=True, blank=True)  # 默认等于空
    end_time = models.DateTimeField(verbose_name="失效时间", null=True, blank=True)
    create_time = models.DateTimeField(verbose_name="订单创建时间", auto_now_add=True)
    modify_time = models.DateTimeField(verbose_name="订单修改时间", auto_now=True)


# """项目信息表"""
class ProjectInfo(models.Model):
    color_choices = (
        (1, '#56b8eb'),
        (2, '#f28033'),
        (3, '#ebc656'),
        (4, '#a2d148'),
        (5, '#20BFA4'),
        (6, '#7461c2'),
        (7, '#20bfa3'),
    )
    project_name = models.CharField(verbose_name="项目名", max_length=32)
    desc = models.TextField(verbose_name="描述", null=True, blank=True)
    color = models.SmallIntegerField(verbose_name="颜色", choices=color_choices, default=1)
    star_flag = models.BooleanField(verbose_name="星标", default=False)
    participators = models.IntegerField(verbose_name="参与人数", default=1)
    create_user = models.ForeignKey(UserInfo, on_delete=models.CASCADE,
                                    verbose_name="创建者")  # 级联删除 on_delete=models.CASCADE
    used_size = models.BigIntegerField(verbose_name='已使用空间', null=True, blank=True, help_text='字节')
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)


# """项目参与者信息表"""
class ParticipatorInfo(models.Model):
    project = models.ForeignKey(ProjectInfo, verbose_name="项目")
    participator = models.ForeignKey(UserInfo, verbose_name="参与者")
    star_flag = models.BooleanField(verbose_name="星标", default=False)
    inviter = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='inviter', verbose_name='邀请者')
    create_time = models.DateTimeField(verbose_name="加入时间", auto_now_add=True)
    status = models.SmallIntegerField(verbose_name="状态", default=1)

# ''' wiki文章表 '''
class Wiki(models.Model):
    title = models.CharField(verbose_name='标题', max_length=32)
    project = models.ForeignKey(to='ProjectInfo', verbose_name='项目', on_delete=models.CASCADE)
    content = models.TextField(verbose_name='内容')
    parent = models.ForeignKey(to='self', verbose_name='上级文章', null=True, blank=True, related_name='children')
    depth = models.IntegerField(verbose_name="深度", default=1)

    def __str__(self):
        return self.title


# ''' 文件库 '''
class FileRepository(models.Model):
    project = models.ForeignKey(to='ProjectInfo', verbose_name='项目', on_delete=models.CASCADE)
    file_type_choices = (
        (1, '文件'),
        (2, '文件夹'),
    )
    file_type = models.SmallIntegerField(verbose_name='类型', choices=file_type_choices)
    name = models.CharField(verbose_name='文件/文件夹名称', max_length=128, help_text='文件/文件夹名称')
    key = models.CharField(verbose_name='文件存储的名称', max_length=128, null=True, blank=True)
    file_size = models.BigIntegerField(verbose_name='文件大小', null=True, blank=True, help_text='字节')
    file_path = models.CharField(verbose_name='文件存储路径', null=True, blank=True, max_length=255)
    parent = models.ForeignKey(verbose_name='父级目录', to='self', related_name='child', null=True, blank=True)
    update_user = models.ForeignKey(verbose_name='最近更新者', to='UserInfo')
    update_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)


# ''' 问题类型 '''
class IssuesType(models.Model):
    PROJECT_INIT_LIST = ['问题', '功能', 'Bug']
    title = models.CharField(verbose_name="类型名称", max_length=32)
    project = models.ForeignKey(verbose_name="所属项目", to='ProjectInfo')

    def __str__(self):
        return self.title


# ''' 问题所属里程碑 '''
class Milestone(models.Model):
    title = models.CharField(verbose_name="里程碑名称", max_length=32)
    project = models.ForeignKey(verbose_name="所属项目", to='ProjectInfo')

    def __str__(self):
        return self.title


# ''' 问题 '''
class Issues(models.Model):
    project = models.ForeignKey(verbose_name="所属项目", to='ProjectInfo')
    issues_type = models.ForeignKey(verbose_name="问题类型", to='IssuesType')
    milestone = models.ForeignKey(verbose_name="里程碑", to='Milestone', null=True, blank=True)
    subject = models.CharField(verbose_name="主题", max_length=80)
    desc = models.TextField(verbose_name="问题描述")
    priority_choices = (
        ("danger", "高"),
        ("warning", "中"),
        ("success", "低"),
    )
    priority = models.CharField(verbose_name="优先级别", max_length=12, choices=priority_choices, default='success')
    # 新建、处理中、已解决、已忽略、待反馈、已关闭、重新打开
    status_choices = (
        (1, '新建'),
        (2, '处理中'),
        (3, '已解决'),
        (4, '已忽略'),
        (5, '待反馈'),
        (6, '已关闭'),
        (7, '重新打开'),
    )
    status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)
    assign = models.ForeignKey(verbose_name='指派', to='UserInfo', related_name='task', null=True, blank=True)
    attention = models.ManyToManyField(verbose_name='关注者', to='UserInfo', related_name='observe', blank=True)

    start_date = models.DateField(verbose_name='开始时间', null=True, blank=True)
    end_date = models.DateField(verbose_name='结束时间', null=True, blank=True)
    mode_choices = (
        (1, '公开模式'),
        (2, '隐私模式'),
    )
    mode = models.SmallIntegerField(verbose_name='模式', choices=mode_choices, default=1)

    parent = models.ForeignKey(verbose_name='父问题', to='self', related_name='child', null=True, blank=True,
                               on_delete=models.SET_NULL)

    creator = models.ForeignKey(verbose_name='创建者', to='UserInfo', related_name='create_problems')

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    latest_update_datetime = models.DateTimeField(verbose_name='最后更新时间', auto_now=True)

    def __str__(self):
        return self.subject

# ''' 问题回复 '''
class IssuesReply(models.Model):
    reply_type_choices = (
        (1, "修改记录"),
        (2, "回复"),
    )
    reply_type = models.IntegerField(verbose_name="类型", choices=reply_type_choices)
    issues = models.ForeignKey(verbose_name="问题", to='Issues')
    content = models.TextField(verbose_name="描述")
    creator = models.ForeignKey(verbose_name="创建者", to="UserInfo", related_name="create_reply")
    create_datetime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    reply = models.ForeignKey(verbose_name="回复", to="self", null=True, blank=True)

# ''' 项目邀请码 '''
class ProjectInvite(models.Model):
    project = models.ForeignKey(verbose_name="所属项目", to='ProjectInfo')
    code = models.CharField(verbose_name="邀请码", unique=True, max_length=64)
    count = models.PositiveIntegerField(verbose_name="限制数量", null=True, blank=True, help_text="空表示无数量限制")
    use_count = models.PositiveIntegerField(verbose_name="已邀请数量", default=0)
    validity_period_choices = (
        (30, '30分钟'),
        (60, '1小时'),
        (300, '5小时'),
        (1440, '24小时'),
    )
    validity_period = models.IntegerField(verbose_name="有效期",choices=validity_period_choices,default=60)
    create_datetime = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)
    creator = models.ForeignKey(verbose_name="创建者",to='UserInfo',related_name='create_invite')
